﻿
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>
<div ng-controller="ChangePasswordController">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <!-- START panel-->
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
                        <div class="panel-body">
                            <fieldset class="b0">
                                <legend>修改密码</legend>
                            </fieldset>
                            <fieldset>
                                <div class="form-ingroup">
                                    <label class="col-sm-2 control-label">原密码</label>
                                    <div class="col-sm-4">
                                        <input type="password" name="oldPassword" required="" ng-model="PasswordContent.oldPassword" class="form-control" maxlength="20" />
                                    </div>
                                    <div class="col-sm-2">
                                        <h4 class="text-danger" style="color:red">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-ingroup">
                                    <label class="col-sm-2 control-label">新密码</label>
                                    <div class="col-sm-4">
                                        <input type="password" name="newPassword" required="" ng-model="PasswordContent.newPassword" class="form-control" maxlength="20" />
                                    </div>

                                    <div class="col-sm-2">
                                        <h4 class="text-danger" style="color:red">*</h4>
                                    </div>
                                    <span ng-show="IsEnoughLen" class="text-danger">密码长度最少6位，最多20位</span>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">确认密码</label>
                                    <div class="col-sm-4">
                                        <input type="password" name="sureNewPassword" required="" ng-model="PasswordContent.sureNewPassword" class="form-control" maxlength="20" />
                                    </div>
                                    <span ng-show="IsSamePassword" class="text-danger">两次密码不一致</span>
                                    <div class="col-sm-2">
                                        <h4 class="text-danger" style="color:red">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                        <div class="panel-footer text-right">
                            <button type="button" class="btn btn-warning" ng-click="cancel()">返回首页</button>
                            <button type="button" class="btn btn-primary" ng-click="serveThisMessage()">保存</button>
                        </div>
                    </div>
                    <!-- END panel-->
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    App.controller('ChangePasswordController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter",
        function ($scope, $state, $http, $stateParams, Notify, $filter) {
            'use strict';
            $scope.isLoading = false;
            $scope.IsSamePassword = false;
            $scope.IsEnoughLen = false;
            $scope.PasswordContent = [];


            //获取密码长度
            $scope.getPwdLength = function (password) {
                if (password == null) { $scope.passwodrLen = 0; }
                else {
                    $scope.passwodrLen = password.replace(/[^x00-xff]/g, "01").length;
                }
            }

            //确认点击
            $scope.serveThisMessage = function () {
                if (!$scope.PasswordContent.oldPassword || $scope.PasswordContent.oldPassword.length == 0) {
                    Notify.alert("请输入原密码", { status: 'warning' });
                    return false;
                }
                if (!$scope.PasswordContent.newPassword || $scope.PasswordContent.newPassword.length == 0) {
                    Notify.alert("请输入新密码", { status: 'warning' });
                    return false;
                }
                if (!$scope.PasswordContent.sureNewPassword || $scope.PasswordContent.sureNewPassword.length == 0) {
                    Notify.alert("请输入确认密码", { status: 'warning' });
                    return false;
                }
                $scope.getPwdLength($scope.PasswordContent.newPassword);
                if ($scope.passwodrLen < 6) {
                    $scope.IsEnoughLen = true;
                    if ($scope.PasswordContent.sureNewPassword == $scope.PasswordContent.newPassword) {
                        $scope.IsSamePassword = false;
                    }
                    return false;
                } else {
                    $scope.IsEnoughLen = false;
                }
                if ($scope.PasswordContent.sureNewPassword != $scope.PasswordContent.newPassword) {
                    $scope.IsSamePassword = true;
                    return false;
                } else {
                    $scope.IsSamePassword = false;
                }
                $scope.isLoading = true;
                $http.post("/User/ChangePassword", { OldPassword: $scope.PasswordContent.oldPassword, NewPassword: $scope.PasswordContent.newPassword }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        $state.go("app.homepage");
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                    $scope.isLoading = false;
                });
            }

            //返回首页
            $scope.cancel = function () {
                $state.go("app.homepage");
            }
        }]);
</script>
